﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DBUnitility;
using Models;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
    public class AGVInfoService
    {
        #region 查询
        /// <summary>
        /// 获取AGV基本信息
        /// </summary>
        /// <returns></returns>
        public List<AGVInfo> GetAGVInfo(string agvStatus)
        { 
            //【1】SQL语句
            string sql = "select AGVId,AGVName,AGVStatus,IP,Port,AGVInfo.TypeID,TypeName,AGVInfo.TrackId,TrackName from AGVInfo ";
            sql += "inner join AGVType on AGVType.TypeID = AGVInfo.TypeID ";
            sql += "inner join TrackInfo on TrackInfo.TrackId = AGVInfo.TrackId";
            if(agvStatus != "")
            sql += " where AGVStatus = 1";
            List<AGVInfo> list = new List<AGVInfo>();
            try
            {
                //【2】后台数据访问
                SqlDataReader objReader = SQLHelper.GetReader(sql);
                while (objReader.Read())
                {
                    list.Add(new AGVInfo()
                    {
                        AGVId =Convert.ToInt32(objReader["AGVId"]),
                        AGVName =objReader["AGVName"].ToString(),
                        IP =objReader["IP"].ToString(),
                        Port = Convert.ToInt32(objReader["Port"]),
                        TypeID=Convert.ToInt32(objReader["TypeID"]),
                        TypeName = objReader["TypeName"].ToString(),
                        TrackId = Convert.ToInt32(objReader["TrackId"]),
                        TrackName = objReader["TrackName"].ToString(),
                        AGVStatus = Convert.ToInt32(objReader["AGVStatus"])
                    });
                }
                objReader.Close();//关闭连接
                return list;
            }
            catch (Exception ex)
            {
                
                throw new Exception("AGV信息查询异常，异常信息："+ex.Message);
            }
            
        }
        #endregion

        #region 增加
        /// <summary>
        /// 新增AGV信息
        /// </summary>
        /// <param name="objAGVInfo"></param>
        /// <returns></returns>
        public int AddAGVInfo(AGVInfo objAGVInfo)      
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@AGVId",objAGVInfo.AGVId),
                new SqlParameter("@AGVName",objAGVInfo.AGVName),
                new SqlParameter("@IP",objAGVInfo.IP),
                new SqlParameter("@Port",objAGVInfo.Port),
                new SqlParameter("@AGVStatus",objAGVInfo.AGVStatus),
                new SqlParameter("@TrackId",objAGVInfo.TrackId),
                new SqlParameter("@TypeID",objAGVInfo.TypeID)
            };
            int result = SQLHelper.UpdateByProcedure("usp_AddAGVInfo", param);
            return result;
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改AGV信息
        /// </summary>
        /// <param name="objAGVInfo"></param>
        /// <returns></returns>
        public int ModifyAGVInfo(AGVInfo objAGVInfo)
        { 
            //SQL参数初始化
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@AGVId",objAGVInfo.AGVId),
                new SqlParameter("@AGVName",objAGVInfo.AGVName),
                new SqlParameter("@IP",objAGVInfo.IP),
                new SqlParameter("@Port",objAGVInfo.Port),
                new SqlParameter("@AGVStatus",objAGVInfo.AGVStatus),
                new SqlParameter("@TrackId",objAGVInfo.TrackId),
                new SqlParameter("@TypeID",objAGVInfo.TypeID),
            };
            return SQLHelper.UpdateByProcedure("[usp_ModifyAGVInfo]", param);
        }
        #endregion

        #region 删除
        /// <summary>
        /// 按AGVID号删除信息
        /// </summary>
        /// <param name="agvId"></param>
        /// <returns></returns>
        public int DeleteAGVInfo(string agvId)
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@AGVId",agvId)
            };
            return SQLHelper.UpdateByProcedure("usp_DeleteAGVInfo",param);
        }
        #endregion

        /// <summary>
        /// 判断该IPAGV是否已经存在
        /// </summary>
        /// <param name="ip">IP地址</param>
        /// <returns>存在数量</returns>
        public int IsExistsAGVByIP(string ip)
        {
            string sql = "select count(*) from AGVInfo where IP ='{0}'";
            sql = string.Format(sql,ip);
            return Convert.ToInt32(SQLHelper.GetSingalResult(sql));
        }

        /// <summary>
        /// 添加agv信息保存到数据库
        /// </summary>
        /// <param name="aGV"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public int AddAGVMessage(AGV aGV)
        {
            string sql = "insert into AGV(Process_Code,Device_ID,AGVID,CurrentLocalLable,CurrentSite,CurrentSpeed,AGVState,ElectricQuantity,date_time) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')";
            sql = string.Format(sql, aGV.Process_Code, aGV.Device_ID, aGV.AGVID, aGV.Currentid, aGV.CurrentSite, aGV.CurrentSpeed, aGV.AGVState, aGV.ElectricQ, aGV.date_time);


            try
            {
                return SqlHeplerT.Update(sql);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// 添加agv信息保存到数据库
        /// </summary>
        /// <param name="aGV"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        ////public int AddAGVMessage(AGV aGV)
        ////{
        ////    string sql = "insert into AGV(Factory_Name,Workshop_Name,Production_Line,Process_Code,Device_ID,AGVID,Currentid,CurrentSite,CurrentSpeed,AGVState,ElectricQ,date_time) values('null','null','null','{7}','{8}','{0}','{1}','{2}','{3}','{4}','{5}','{6}')";
        ////    sql = string.Format(sql, aGV.AGVID, aGV.Currentid, aGV.CurrentSite, aGV.CurrentSpeed, aGV.AGVState, aGV.ElectricQ, aGV.date_time, aGV.Process_Code, aGV.Device_ID);


        ////    try
        ////    {
        ////        return SQLHelper.Update(sql);
        ////    }
        ////    catch (Exception ex)
        ////    {
        ////        throw new Exception(ex.Message);
        ////    }
        ////}
        /// <summary>
        /// 根据IP查找AGV编号
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public string GetAGvid(string ip)
        {
            string sql = "select  AGVId  from  AGVInfo where IP='" + ip + "' ";
            try
            {
                var id = SQLHelper.GetSingalResult(sql);
                return id.ToString();
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
        /// <summary>
        /// 根据IP查找AGV名称
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public string GetAGvName(string ip)
        {
            string sql = "select  AGVName  from  AGVInfo where IP='" + ip + "' ";
            try
            {
                var Name = SQLHelper.GetSingalResult(sql);
                return Name.ToString();
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        public DataSet GetAgvip()
        {
            string sql = "select IP from  AGVInfo  where AGVStatus=1";
            try
            {
                return SQLHelper.GetDataSet(sql);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
